import mongoose = require('mongoose');
import q = require('bluebird');

mongoose.Promise = q;


import User = require("../model/UserInfo");

export default class AddUser{
    public addOne(username:string, password:string):q<boolean>{
        let me = this;
        return me.checkUserDuplicate(username).then((duplicate:boolean)=>{
            if(duplicate){
                return false;
            }
            let inst = new User.MODEL({
                "username":username,"password":password
            });
            return inst.save().then((ret)=>{
                if(ret){
                    return true;
                }               
                else{
                    return false;
                } 
            });
        });
    }

    private checkUserDuplicate(username:string):q<boolean>{
        let q = User.MODEL.findOne({"username":username}).exec();

        return q.then((ret)=>{
            if(ret){
                return true;
            }
            return false;
        });
    }
}